package com.huabo.fxgl.controller;

import com.huabo.fxgl.entity.Organization;
import com.huabo.fxgl.entity.Staff;
import com.huabo.fxgl.entity.YyTeam;
import com.huabo.fxgl.service.IYyTeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/fxyj")
@Api(tags = {"风险监控 - 供应商监控"})
@Slf4j
public class YyTeamController {

    @Autowired
    private IYyTeamService yyTeamService;

    /**
     * http://huabodashuju.com/fxyj/saveteam?groupname=%E6%B5%8B%E8%AF%95%E5%88%86%E7%BB%84&teamid=654887
     * 请求方法: POST
     * groupname: 测试分组
     * teamid: 654887
     *
     * @param teamid 分组ID
     * @param groupname 分组名称
     * @return
     */
    @RequestMapping("/saveteam")
    @ApiOperation("风险监控 - 供应商监控 - 保存分组（新增和修改） /fxyj/saveteam")
    public String saveTeam(@ApiParam(name = "teamid", value = "teamid") @RequestParam(required = false) String teamid,
                           @ApiParam(name = "groupname", value = "groupname") @RequestParam String groupname,
                           @ApiParam(name = "staff", value = "staff") Staff staff,
                           @ApiParam(name = "organization", value = "organization") Organization organization) {
        if (StringUtils.isNotBlank(teamid)) {
            //若teamID不为空即为修改其分组名称
            YyTeam team = yyTeamService.getById(teamid);
            team.setTeamname(groupname);//修改分组名称
            yyTeamService.updateById(team);
            return teamid;
        } else {
            //若teamid为提供，认为是新增分组
            YyTeam team = new YyTeam(null, groupname, LocalDateTime.now(), staff, organization, 0L);
            log.info("Team info: " + team);
            yyTeamService.save(team);
            return String.valueOf(team.getTeamid());
        }
    }
}
